import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_demo_k/view/custom_app_bar.dart';

import '../view/card_view_control.dart';

class PageViewCount extends StatefulWidget {
  static const String routeName = '/page_view_count';
  const PageViewCount({super.key});

  @override
  State<PageViewCount> createState() => _PageViewCountState();
}

class _PageViewCountState extends State<PageViewCount> {
  PageController _pageController = PageController();
  SwiperController _swiperController = SwiperController();
  int currentIndex = 0;
  double downPointDx = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(title: '测试1'),
        body: PageView(
          controller: _pageController,
          children: [
            const Center(
              child: Text(
                'Page 1',
                style: TextStyle(fontSize: 36),
              ),
            ),
            Listener(
              onPointerDown: (e) {
                print('onPointerDown....${e}');
                downPointDx = e.position.dx;
              },
              onPointerMove: (e) {
                if (currentIndex == 0) {
                  // _pageController.previousPage(
                  //     duration: Duration(milliseconds: 300),
                  //     curve: Curves.ease);
                  double width = MediaQuery.of(context).size.width;

                  if(downPointDx<e.position.dx){
                    _pageController.jumpTo(width - e.position.dx);
                  }

                }
              },
              child: Swiper(
                controller: _swiperController,
                itemBuilder: (BuildContext context, int index) {
                  return Image.network("https://via.placeholder.com/350x150",
                      fit: BoxFit.cover);
                },
                itemCount: 3,
                loop: false,
                onIndexChanged: (e) {
                  currentIndex = e;
                },
                // pagination: SwiperPagination(),
                // control: SwiperControl(),
              ),
            ),
          ],
        ));
  }
}
